<?php


namespace App\Http\Middleware;


use App\Handlers\DeviceTypeHandlers;
use App\Models\AccessLog;
use App\Services\ConfigService;
use Illuminate\Support\Arr;


class GooglebotChecked
{
    public function handle($request, \Closure $next){

        $this->accessLog($request,200);

        if($request->path() == '/'){
            $user_agent  = $request->userAgent();
            if(strpos(strtolower($user_agent),'googlebot') !== false){
                $close_googlebot = ConfigService::get('close_googlebot');
                if($close_googlebot){
                    return response('','500');
                }else{
                    $googlebot_index_page = ConfigService::get('googlebot_index_page');
                    if($googlebot_index_page){
                        echo $googlebot_index_page;exit;
                    }
                }

            }

        }

        return $next($request);

    }

    private function accessLog($request,$response){
        $data = [
            'url'=>$request->path(),
            'method'=>$request->method(),
            'host'=>$request->getHost(),
            'referer'=>Arr::get($_SERVER,'HTTP_REFERER'),
            'ip'=>$request->header('cf-connecting-ip',$request->ip()),
            'user_agent'=>$request->userAgent(),
            'device'=>DeviceTypeHandlers::getDevice($request->userAgent()),
            'crawler'=>DeviceTypeHandlers::getCrawler($request->userAgent()),
            'parameter'=>$request->toArray(),
            'headers'=>$request->header(),
            'response'=>$response,
        ];

        AccessLog::create($data);
    }
}
